# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.

# openai
from time import sleep

import openai
from wxpy import *
# Organization
openai.organization = "YOUR_organization"
# api_key
openai.api_key = "YOUR_api_key"
toxic_threshold = -0.355
openai.Model.list()

show_str = ""
def getChatRetrun(content_to_classify):

    response = openai.Completion.create(
        model="text-davinci-003",
        # model="code-davinci-002",
        prompt=content_to_classify,
        temperature=0.9,
        max_tokens=2048,
        top_p=1,
        frequency_penalty=0.0,
        presence_penalty=0.6,
        stream=False
    )
    # print("\n机器人：\n")
    # for completion in response:
    #     print(completion["choices"][0]["text"],end="")
    #     # show_str+=completion["choices"][0]["text"]
    return response["choices"][0]["text"]
'''
微信登录，回复操作函数
'''

# 要自动回复的群个数
groupNames = []
groups = []
print("请登录微信，扫码后稍等片刻")
bot = Bot()  # 获取所有好友
sleep(1)
num = input('\n您要自动回复几个群(数字):')
if(int(num)==0):
    exit(0)
for i in range(int(num)):
    group_name = input('输入需自动回复的群{}名称:'.format(i+1))
    if(i==0):
        print("正在加载好友列表，过程较慢，请稍等！！！")
    groupNames.append(group_name)
    if(len(bot.groups().search(group_name))>0):
        groups.append(bot.groups().search(group_name)[0])
        print("[{}]设置成功".format(group_name))
    else:
        print("[{}]设置失败，未找到该名称的群".format(group_name))


# group_name2  = input('输入需自动回复的群名称2:')

my_msg_head = r'@{}'.format(bot.self.nick_name)
print("设置完成，可以开始使用了，以[{}]开头就会回复".format(my_msg_head))

# group2 = bot.groups().search(group_name2)[0]

def getReplayName(msg):
    display_name = msg.member.display_name
    if (msg.member.is_friend == False):
        pass
    elif (len(msg.member.display_name) == 0):
        display_name = bot.search(msg.member.name)[0].nick_name
    else:
        pass
    return display_name
#指定好友
# friend = bot.friends().search(\'好友名\')[0]
#按需求修改参数
@bot.register(groups,msg_types=TEXT,except_self=False)
def gourp1_reply(msg):
    print("{}:{}".format(msg.member.name,msg.text))
    if(msg.text.startswith(my_msg_head)):
        send_str = getChatRetrun(msg.text.replace(my_msg_head,''))
        send_final = "@{} {}".format(getReplayName(msg), send_str)
        print('{}:{}'.format(getReplayName(msg),send_final))
        return send_final

if __name__ == '__main__':
    # while(1):
    #     text = input("\n我:")
    # getChatRetrun('Shell 遍历文件怎么写')
    bot.join()